package exception.test;


import java.util.Scanner;

public class Customize {
    public static void main(String[] args) throws SexException {
        Person person = new Person();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入性别");
        String sex = sc.next();
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        person.setName(name);
        person.setSex(sex);
        person.setAge(age);
        person.print();
    }
}

class Person {
    private int age;
    private String sex;
    private String name;

    public Person() {
    }

    public Person(int age, String sex, String name) {
        this.age = age;
        this.sex = sex;
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if (age < 1 || age > 120) {
            try {
                throw new AgeException("年龄错误");
            } catch (AgeException e) {
                System.err.println(e.getMessage());
                this.age = 18;
            }
        } else
            this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) throws SexException {
        if (("男".equals(sex) || "女".equals(sex)))
            this.sex = sex;
        else {//getMessage
            try {
                throw new SexException("性别错误");
            } catch (SexException e) {
                System.err.println(e.getMessage());
                this.sex = "男";
            }
        }
    }

    public void print() {
        System.out.println("name " + name + " age: " + age + " sex: " + sex);
    }

}

//自定义年龄异常 属于运行时异常
class AgeException extends RuntimeException {
    public AgeException(String message) {
        super(message);
    }
}

//自定义性别异常 属于检查性异常
class SexException extends Exception {
    public SexException(String message) {
        super(message);
    }
}